package com.sptpc.alertdialogdemo;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.w3c.dom.Text;

public class MyDialog extends Dialog {
    public MyDialog(@NonNull Context context) {
        super(context);
    }
    public static class Builder{
        private Context context;
        private TextView mtitle;
        private TextView mMessage;
        private TextView positiveButtonText;
        private TextView negativeButtonText;
        private View contentView;
//        private DialogInterface.OnClickListener positiveButtonClickListener;
//        private DialogInterface.OnClickListener negativeButtonClickListener;
        private MyDialog myDialog;

        public Builder(final Context context){
            this.context = context;
            myDialog = new MyDialog(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            contentView = inflater.inflate(R.layout.mydialog, null, false);
            myDialog.addContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                      ViewGroup.LayoutParams.MATCH_PARENT));
            mtitle = contentView.findViewById(R.id.title);
            mMessage = contentView.findViewById(R.id.message);
            positiveButtonText =contentView.findViewById(R.id.positiveTextView);
            negativeButtonText = contentView.findViewById(R.id.negativeTextView);
            negativeButtonText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"你选择了取消",Toast.LENGTH_SHORT).show();;
                    myDialog.dismiss();
                }
            });
            positiveButtonText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"你选择了确定",Toast.LENGTH_SHORT).show();;
                    myDialog.dismiss();
                }
            });
        }

        public Builder setMessage(String message) {
            mMessage.setText(message);
            return this;
        }

        public Builder setNegativeButton(TextView negativeButtonText) {
            negativeButtonText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"你选择了确定",Toast.LENGTH_SHORT).show();;
                    myDialog.dismiss();
                }
            });
            return this;
        }

        public Builder setPositiveButton(TextView positiveButtonText) {
            positiveButtonText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"你选择了取消",Toast.LENGTH_SHORT).show();;
                    myDialog.dismiss();
                }
            });
            return this;
        }

        public Builder setTitle(String title) {
            mtitle.setText(title);
            return this;
        }

        public MyDialog create() {
            myDialog.setContentView(contentView);
            return myDialog;
            }


    }
}
